/**
 * 
 */
package com.zhiteng.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zhiteng.entity.PointLimit;
import com.zhiteng.entity.Teacher;
import com.zhiteng.entity.TypeLimit;
import com.zhiteng.mapper.TeacherMapper;
import com.zhiteng.service.PointLimitService;
import com.zhiteng.service.TeacherService;
import com.zhiteng.service.TypeLimitService;

/**  
* <p>Title: TeacherServiceImpl.java</p>  
* <p>Description:教师Service </p>  
* @author dragon  
* @date 2018年11月7日  
* @version 1.0  
*/
@Service
public class TeacherServiceImpl implements TeacherService {
	@Autowired
	private TeacherMapper teacherMapper;
	@Autowired
	private PointLimitService pointLimitService;
	@Autowired
	private TypeLimitService typeLimitService;

	@Override
	public int saveTeacherBatch(List<Teacher> teacherList) {
		int count = 0;
		for (Teacher teacher : teacherList) {
			if(teacherMapper.existsWithPrimaryKey(teacher.getUserid())) {	//	已存在，更新
				teacherMapper.updateByPrimaryKeySelective(teacher);
			}else {	//	不存在，插入
				teacher.setBonusPoint(0); 	//	设置教师初始积分为0
				teacherMapper.insert(teacher);
			}
			++count;
		}
		return count;
	}

	@Override
	public List<Teacher> queryTeacherByTeacherNameAndSchoolName(String schoolName, String teacherName) {
		Teacher t = new Teacher();
		t.setSchool_name(schoolName);
		t.setName(teacherName);
		return teacherMapper.select(t);
	}

	@Override
	public Teacher queryTeacherByCellphoneAndSchoolName(String cellphone, String schoolName) {
		Teacher t = new Teacher();
		t.setCellphone(cellphone);
		t.setSchool_name(schoolName);;
		return teacherMapper.selectOne(t);
	}

	@Override
	public List<Teacher> queryTeacherByTelephone(String telephone) {
		Teacher t = new Teacher();
		t.setCellphone(telephone);
		return teacherMapper.select(t);
	}

	@Override
	public Teacher queryTeacherById(String teacher_id) {
		return teacherMapper.selectByPrimaryKey(teacher_id);
	}

	@Override
	public List<Teacher> queryAllTeacherBySchoolName(String schoolName) {
		Teacher teacher = new Teacher();
		teacher.setSchool_name(schoolName);
		return teacherMapper.select(teacher);
	}
	

	@Override
	public int updateTeacher(Teacher teacher) {
		return teacherMapper.updateByPrimaryKeySelective(teacher);
	}

	@Override
	public int updateBonusPoint(String teacherId, Integer type) {
		//	查询该type对应的积分上限和每次操作需要加的积分数
		TypeLimit typeLimit = typeLimitService.queryTypeLimitByType(type);
		if(typeLimit==null) return -2;
		int oncePoint = typeLimit.getOncePoint();	//	每次操作加分多少
		int topLimit = typeLimit.getToplimit();	//	一个周期内积分上限
		
		Teacher teacher = teacherMapper.selectByPrimaryKey(teacherId);
		if(teacher==null) return -1;;
		
		PointLimit pointLimit = pointLimitService.queryPointLimitByTeacherIdAndType(teacherId,type);
		if(pointLimit==null) {	// 说明该教师从来没有加过积分
			teacher.setBonusPoint(teacher.getBonusPoint()+oncePoint);
			
			pointLimit = new PointLimit();
			pointLimit.setTeacherId(teacherId);
			pointLimit.setType(type);
			pointLimit.setCurrCyclePoint(oncePoint);
			pointLimitService.savePointLimit(pointLimit);
			
			return teacherMapper.updateByPrimaryKeySelective(teacher);
		}else {
			if(pointLimit.getCurrCyclePoint()<topLimit) {
				pointLimit.setCurrCyclePoint(pointLimit.getCurrCyclePoint()+oncePoint);
				teacher.setBonusPoint(teacher.getBonusPoint()+oncePoint);
				pointLimitService.updatePointLimit(pointLimit);
				
				return teacherMapper.updateByPrimaryKeySelective(teacher);
			}else {	// 已达积分上限
				return -3;
			}
		}
	}
	
	/////////////////////////////////////////////////////////
	
	@Override
	public List<Teacher> queryTeacherByNameAndTelephone(String name, String telephone) {
		Teacher record = new Teacher();
		record.setCellphone(telephone);
		record.setName(name);

		List<Teacher> list = teacherMapper.select(record);

		return list;
	}
	@Override
	public Teacher queryTeacherByTeacherId(String teacher_id) {

		Teacher record = new Teacher();
		record.setUserid(teacher_id);
		return teacherMapper.selectOne(record);
	}

	

}
